package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDownOrUpDto;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import io.protostuff.Request;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController {

    @Autowired
    private WmNewsService wmNewsService;

    @PostMapping("/list")
    public ResponseResult list(@RequestBody WmNewsPageReqDto dto){

        if(dto.getEndPubDate() != null){
            dto.setEndPubDate(dto.getEndPubDate().plusDays(1));
        }

//        dto.setBeginPubDate(dto.getBeginPubDate() + " 00:00:00");
//        dto.setEndPubDate(dto.getEndPubDate() + " 23:59:59");
        //1.参数校验
        dto.checkParam(); //page size合理化

        return wmNewsService.findPage(dto);
    }

    @PostMapping("/submit")
    public ResponseResult submit(@Valid @RequestBody WmNewsDto dto){
        //明早解决参数校验通用性问题  写案例
        wmNewsService.submit(dto);
        return ResponseResult.okResult(null);
    }

    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDownOrUpDto dto){
        wmNewsService.downOrUp(dto);
        return ResponseResult.okResult(null);
    }
}